/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.persistence.*;

/**
 *
 * @author Manolis
 */
public class ExternalDBManager {

    
    
    private static EntityManager exm;
    private static Connection conn2;
    private static final String URL = "jdbc:derby://plh24.ecomet.eap.gr:8080/CREDIT_CARD_AUTH_DB;user=student;password=studentpass";
    private static final String driver = "org.apache.derby.jdbc.ClientDriver";
    private static final String unitName = "CREDIT_CARD_AUTH_DB";
    
    //o constructor
     public ExternalDBManager(){
          try {
            Class.forName(driver).newInstance();
            //Get a connection
            conn2 = DriverManager.getConnection(URL);
            EntityManagerFactory exmf = javax.persistence.Persistence.createEntityManagerFactory(unitName);
            exm = exmf.createEntityManager();
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException except) {
            System.out.println("ERROR: " + except.getMessage());
        }
        
    }
    
    public static void shutdown() {
        try {
            if (conn2 != null) {
                DriverManager.getConnection(URL + ";shutdown=true");
                conn2.close();
            } 
        } catch (SQLException sqlExcept) {
        }
    }   
    
    //επιστρέφει τον entity manager 
    public static EntityManager getEm() {
        return exm;
    }
    
}
